
package com.changjiang.luban.utils;

import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 描述:日期工具类
 * @author nichangjiang@vip.qq.com
 * 创建时间: 2017/12/22. 8:49
 * 修改人:
 * 修改时间:
 * 修改备注:
 * 版本:v1.0
 */
public class DateUtils {


    public static final String DATE_FORMAT_DEFAULT = "yyyy-MM-dd";

    public static final String DATE_FORMAT_COMPLETE = "yyyy-MM-dd HH:mm:ss";

    public static final String DATE_FORMAT_COMPLETE_S = "yyyy-MM-dd HH:mm:ss.S";

    public static final String DATE_FORMAT_FULL_DEFAULT = "yyyyMMdd";

    public static final String DATE_FORMAT_FULL_YM = "yyyyMM";

    public static final String DATE_FORMAT_FULL_COMPLETE = "yyyyMMddHHmmss";

    public static final String DATE_FORMAT_FULL_COMPLETE_S = "yyyyMMddHHmmssSSS";

    public static final String DATE_FORMAT_CHINA_DEFAULT ="yyyy年MM月dd日";

    public static final String DATE_FORMAT_CHINA_COMPLETE = "yyyy年MM月dd日HH时mm分ss秒";

    public static final String DATE_FORMAT_CHINA_COMPLETE_S = "yyyy年MM月dd日HH时mm分ss秒SSS毫秒";

    private final static SimpleDateFormat SHORT_SDF = new SimpleDateFormat(DATE_FORMAT_DEFAULT);

    private final static SimpleDateFormat LONG_SDF  = new SimpleDateFormat(DATE_FORMAT_COMPLETE);

    private static final long TIME_DAY = 24*3600*1000;

    private static final String TIME_SUFFIX_START = " 00:00:00";

    private static final String TIME_SUFFIX_END = " 23:59:59";

    /**
     * 获取当前系统的时间
     * @return 日期字符串
     */
    public static String getCurrentSystemDate(){
        return getCurrentSystemDate(DATE_FORMAT_DEFAULT);
    }

    /**
     * 获取当前系统的时间并且格式化
     * @param format 格式化字符串
     * @return 日期字符串
     */
    public static String getCurrentSystemDate(String format){
        Date date = new Date();
        return getDateFormat(date,format);
    }

    /**
     * 格式化日期成字符串
     * @param date 日期对象
     * @param format 格式化字符串
     * @return 日期字符串
     */
    public static String getDateFormat(Date date,String format){
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 日期时间字符串格式化
     * @param start YYYY-MM-DD 格式日期字符串
     * @return YYYY-MM-DD + TIME_SUFFIX_START
     */
    public static String getStringStartFormat(String start){
        Date date = stringToDate(start,DATE_FORMAT_DEFAULT);
        String formatDate = dateToString(date,DATE_FORMAT_DEFAULT);
        return formatDate + TIME_SUFFIX_START;
    }

    /**
     * 日期时间字符串格式化
     * @param end YYYY-MM-DD 格式日期字符串
     * @return YYYY-MM-DD + TIME_SUFFIX_END
     */
    public static String getStringEndFormat(String end){
        Date date = stringToDate(end,DATE_FORMAT_DEFAULT);
        String formatDate = dateToString(date,DATE_FORMAT_DEFAULT);
        return formatDate + TIME_SUFFIX_END;
    }

    /**
     * 将插入的日期格式转换成对应的日期对象
     * @param dateTime 日期
     * @param format 格式化字符串
     * @return 日期字符串
     */
    public static String dateToString(Date dateTime, String format) {
        String str = null;
        try {
            SimpleDateFormat formater = new SimpleDateFormat(format);
            str = formater.format(dateTime);
        } catch (Exception ex) {

        }
        return str;
    }

    /**
     * 将插入的字符串按格式转换成对应的日期对象
     * @param dateStr 日期字符串
     * @param format 格式化字符串
     * @return 日期字符串
     */
    public static Date stringToDate(String dateStr,String format){
        Date dateTime = null;
        try {
            if (StringUtils.isNotBlank(dateStr)){
                SimpleDateFormat formater = new SimpleDateFormat(format);
                dateTime = formater.parse(dateStr);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return dateTime;
    }

    /**
     * 获取当前日期，线程同步
     * @param format 格式化字符串
     * @return 日期字符串
     */
    public static synchronized String getDateThrowd(String format){
        synchronized (DateUtils.class) {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(new Date());
        }
    }

    /**
     * 返回传入日期对象（date）之后afterDays天数的日期对象
     * @param date 日期对象
     * @param afterDays 往后天数
     * @param format 日期格式
     * @return 日期字符串
     */
    public static String getAfterDay(String date,int afterDays,String format) {
        SimpleDateFormat sdf=new SimpleDateFormat(format);
        Date date1=new Date();
        try {
            date1 = sdf.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        cal.add(Calendar.DATE,afterDays);
        return  sdf.format(cal.getTime());
    }

    /**
     * 返回传入日期对象（date）之前afterDays天数的日期对象
     * @param date 日期对象
     * @param afterDays 往前天数
     * @param format 日期格式
     * @return 日期字符串
     */
    public static String getLessDay(String date,int afterDays,String format) {
        SimpleDateFormat sdf=new SimpleDateFormat(format);
        Date date1=new Date();
        try {
            date1 = sdf.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        cal.add(Calendar.DAY_OF_MONTH,-afterDays);

        return  sdf.format(cal.getTime());
    }

    /**
     * 返回两个日期相差的天数
     * @param date1 日期截止
     * @param date2 日期开始
     * @return int 相差天数
     */
    public static int dateDiff(Date date1, Date date2) {
        long time = getTime(date1)-getTime(date2);
        return timeToDay(time);
    }

    /**
     * 获取当前系统时间毫秒数
     * @return 毫秒数
     */
    public static long getTime(){
        return System.currentTimeMillis();
    }

    /**
     * 获取传入日期的毫秒数
     * @param date 日期对象
     * @return 毫秒数
     */
    public static long getTime(Date date){
        return date.getTime();
    }

    /**
     * 毫秒数转换成天数
     * @param time 毫秒数
     * @return 天数
     */
    public static int timeToDay(long time){
        return (int) (time/TIME_DAY);
    }

    /**
     * 获得本周的第一天，周一
     * @return 日期字符串
     */
    public static Date getCurrentWeekDayStartTime(){
        Calendar c = Calendar.getInstance();
        try {
            int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
            c.add(Calendar.DATE, -weekday);
            c.setTime(DateUtils.LONG_SDF.parse(DateUtils.SHORT_SDF.format(c.getTime()) + TIME_SUFFIX_START));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }

    /**
     * 获得本周的最后一天，周日
     * @return 日期字符串
     */
    public static Date getCurrentWeekDayEndTime() {
        Calendar c = Calendar.getInstance();
        try {
            int weekday = c.get(Calendar.DAY_OF_WEEK);
            c.add(Calendar.DATE, 8 - weekday);
            c.setTime(DateUtils.LONG_SDF.parse(DateUtils.SHORT_SDF.format(c.getTime()) + TIME_SUFFIX_END));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }

    /**
     * 获得本天的开始时间
     * @return 日期字符串
     */
    public static Date getCurrentDayStartTime() {
        Date now = new Date();
        try {
            now = DateUtils.SHORT_SDF.parse(DateUtils.SHORT_SDF.format(now));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 获得本天的结束时间
     * @return 日期字符串
     */
    public static Date getCurrentDayEndTime() {
        Date now = new Date();
        try {
            now = DateUtils.LONG_SDF.parse(DateUtils.SHORT_SDF.format(now) + TIME_SUFFIX_END);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 获得本月的开始时间
     * @return 日期字符串
     */
    public static Date getCurrentMonthStartTime() {
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            c.set(Calendar.DATE, 1);
            now = DateUtils.SHORT_SDF.parse(DateUtils.SHORT_SDF.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 本月的结束时间
     * @return 日期字符串
     */
    public static Date getCurrentMonthEndTime() {
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            c.set(Calendar.DATE, 1);
            c.add(Calendar.MONTH, 1);
            c.add(Calendar.DATE, -1);
            now = DateUtils.LONG_SDF.parse(DateUtils.SHORT_SDF.format(c.getTime()) + TIME_SUFFIX_END);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 当前年的开始时间
     * @return 日期字符串
     */
    public static Date getCurrentYearStartTime() {
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            c.set(Calendar.MONTH, 0);
            c.set(Calendar.DATE, 1);
            now = DateUtils.SHORT_SDF.parse(DateUtils.SHORT_SDF.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 当前年的结束时间
     * @return 日期字符串
     */
    public static Date getCurrentYearEndTime() {
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            c.set(Calendar.MONTH, 11);
            c.set(Calendar.DATE, 31);
            now = DateUtils.LONG_SDF.parse(DateUtils.SHORT_SDF.format(c.getTime()) + TIME_SUFFIX_END);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 当前季度的开始时间
     * @return 日期字符串
     */
    public static Date getCurrentQuarterStartTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3){
                c.set(Calendar.MONTH, 0);
            }
            else if (currentMonth >= 4 && currentMonth <= 6){
                c.set(Calendar.MONTH, 3);
            }
            else if (currentMonth >= 7 && currentMonth <= 9){
                c.set(Calendar.MONTH, 4);
            }
            else if (currentMonth >= 10 && currentMonth <= 12){
                c.set(Calendar.MONTH, 9);
            }else{
                c.set(Calendar.DATE, 1);
            }
            now = DateUtils.LONG_SDF.parse(DateUtils.SHORT_SDF.format(c.getTime()) + TIME_SUFFIX_START);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 当前季度的结束时间
     * @return 日期字符串
     */
    public static Date getCurrentQuarterEndTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                c.set(Calendar.MONTH, 2);
                c.set(Calendar.DATE, 31);
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                c.set(Calendar.MONTH, 8);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            now = DateUtils.LONG_SDF.parse(DateUtils.SHORT_SDF.format(c.getTime()) + TIME_SUFFIX_END);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 判断当前时间是否在时间范围之内
     * @param start 开始范围
     * @param end 结束范围
     * @return
     */
    public static Boolean isCurrentRange(String start,String end){
        Date startDate = stringToDate(start,DATE_FORMAT_COMPLETE);
        Date endDate = stringToDate(end,DATE_FORMAT_COMPLETE);
        Date current = stringToDate(getCurrentSystemDate(DATE_FORMAT_COMPLETE),DATE_FORMAT_COMPLETE);
        return current.after(startDate) && current.before(endDate);
    }
}
